<template>
  <div class="login-container">
    <div class="mask" />
    <div class="wrap">
      <el-row style="width: 900px;">
        <el-col :span="10" :xs="24">
          <div class="logo">
            <!-- <img src="../../assets/logo.png" style="width: 90px;height: 90px;"> --><span class="slogn">后台管理</span>
          </div>
        </el-col>
        <el-col :span="14" :xs="24">
          <div class="form-wrap">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
              <div class="title-container">
                <div class="title">账号登录</div>
              </div>
              <div style="padding: 30px 80px 0;">
                <el-form-item prop="user">
                  <span class="svg-container">
                    <svg-icon icon-class="user" />
                  </span>
                  <el-input ref="user" v-model.trim="loginForm.user" placeholder="账号" name="user" type="text" tabindex="1" autocomplete="on" />
                </el-form-item>
                <el-form-item prop="password">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input :key="passwordType" ref="password" v-model.trim="loginForm.password" :type="passwordType" placeholder="密码" name="password"
                    tabindex="2" autocomplete="on" @keyup.enter.native="handleLogin" />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>
                <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:50px;" @click.native.prevent="handleLogin">登录</el-button>
              </div>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      const validatePassword = (rule, value, callback) => {
        if (value.length < 1 || value.trim().length == 0) {
          callback(new Error('请输入密码'))
        } else if (value.length < 6 || value.length > 20) {
          callback(new Error('请输入6-20位的密码'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          user: undefined,
          password: undefined
        },
        loginRules: {
          user: [{
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            trigger: 'blur',
            validator: validatePassword
          }]
        },
        passwordType: 'password',
        loading: false,
        redirect: undefined,
        otherQuery: {}
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          const query = route.query
          if (query) {
            this.redirect = query.redirect
            this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    },
    created() {
      this.loading = false
      localStorage.clear()
      // window.addEventListener('storage', this.afterQRScan)
    },
    mounted() {
      if (this.loginForm.user === '') {
        this.$refs.user.focus()
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }
    },
    destroyed() {
      // window.removeEventListener('storage', this.afterQRScan)
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                this.$message({
                  message: '登录成功，正在进入系统',
                  type: 'success'
                })
                // setTimeout(() => {
                // this.$router.push({
                // 	path: this.redirect || '/',
                // 	query: this.otherQuery
                // })
                this.$router.push({
                  path: '/',
                  query: this.otherQuery
                })
                // }, 500)
              }).finally(() => {
                this.loading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      }
      // afterQRScan() {
      //   if (e.key === 'x-admin-oauth-code') {
      //     const code = getQueryObject(e.newValue)
      //     const codeMap = {
      //       wechat: 'code',
      //       tencent: 'code'
      //     }
      //     const type = codeMap[this.auth_type]
      //     const codeName = code[type]
      //     if (codeName) {
      //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //         this.$router.push({ path: this.redirect || '/' })
      //       })
      //     } else {
      //       alert('第三方登录失败')
      //     }
      //   }
      // }
    }
  }
</script>

<style lang="scss">
  .login-container {
    .wrap {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 99;
      position: absolute;
      left: 0;
      right: 0;
    }

    .el-input {
      display: inline-block;
      height: 35px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 10px 5px 10px 15px;
        height: 35px;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px #ffffff inset !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid #dcdcdc;
      border-radius: 5px;
      color: #999;
      margin-bottom: 30px;
    }
  }
</style>

<style lang="scss" scoped>
  $dark_gray: #889aa4;

  .login-container {
    position: relative;
    min-height: 100%;
    width: 100%;
    background-image: url("https://img1.baidu.com/it/u=2401320033,3516617462&fm=26&fmt=auto&gp=0.jpg");
    overflow: hidden;

    .mask {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(57, 74, 98, 0.4);
      z-index: 1;
    }

    .logo {
      background-color: rgba(70, 70, 70, 0.5);
      height: 280px;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      max-width: 540px;
      margin: 0 auto;
    }

    .form-wrap {
      background-color: rgba(70, 70, 70, 0.5);
      height: 280px;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 540px;
      margin: 0 auto;
    }

    .slogn {
      color: #ffffff;
      font-size: 30px;
      margin-left: 40px;
      letter-spacing: 4px;
    }

    .login-form {
      background-color: #fdfdfd;
      position: relative;
      width: 400px;
      max-width: 100%;
      // margin-left: 120px;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 0 5px 0 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        color: #aaaaaa;
        padding: 20px;
        border-bottom: 1px solid #efefef;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 0;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }
</style>
